<?php
	// get the instance_id of the newest retired
	$query = "SELECT id FROM n_adc WHERE style = 'retired' AND state < 3";
	$result = mysql_query($query);
	$row = mysql_fetch_assoc($result);
	$id = $row["id"];

	// figure out number of rows in table
	$query = "SELECT * FROM n_adc_" .$id;
	$result = mysql_query($query);
	$num_rows = mysql_num_rows($result);

	// obtain track_id and increase
	$query = "SELECT * FROM n_adc WHERE id=" .$id;
	$result = mysql_query($query);
	$row = mysql_fetch_assoc($result);
	$track_id = $row["track_id"] + 1;

	// save new track_id
	$query = "UPDATE n_adc SET track_id=$track_id WHERE id=" . $id;
	$result = mysql_query($query);

	// add new task
	$data = "cmd=task_add\n";
	$network->send($data);
	while($from = $network->recv()) {
		if(strpos($from, "task_add_reply") !== false) {
			break;
		}
	}
	$pm->init($from, "|");
	$task_id = $pm->get("id");

	$network->send("cmd=task_set|id=$task_id|data=todo|value=adc\n");
	$network->send("cmd=task_set|id=$task_id|data=todo_count|value=$num_rows\n");
	$network->send("cmd=task_set|id=$task_id|data=block_size|value=100\n");
	$network->send("cmd=task_set|id=$task_id|data=timeout|value=30\n");
	$network->send("cmd=task_set|id=$task_id|data=db_user|value="
			. $settings["default"]["db_user"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_pass|value="
			. $settings["default"]["db_pass"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_name|value="
			. $settings["default"]["db_name"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_host|value="
			. $settings["default"]["db_host"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=db_port|value="
			. $settings["default"]["db_port"] ."\n");
	$network->send("cmd=task_set|id=$task_id|data=action|value=daily\n");
	$network->send("cmd=task_set|id=$task_id|data=style|value=retired\n");
	$network->send("cmd=task_set|id=$task_id|data=instance|value=$id\n");
	$network->send("cmd=task_set|id=$task_id|data=priority|value=1\n");

	$network->send("cmd=task_start|id=$task_id\n");
?>
